package org.stvd.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.common.utils.encode.RSAUtil;

import com.wf.captcha.ArithmeticCaptcha;

/**
 * 系统登录-控制类
 * @author houzx
 */
@Controller
public class LoginController {

    /**
     * @Description: 登录请求
     * @param map
     * @param request
     * @return
     */
    @GetMapping(value = "/login")
    public String onLoginLoad(ModelMap map, HttpServletRequest request) {
        String returnUrl =request.getParameter("referer");
        String path = this.getClass().getResource("/rsakey/publicKey.txt").getFile();
        String publicKey = RSAUtil.readTxt(path);
        map.put("publicKey", publicKey);
        map.put("returnUrl", returnUrl);
        return "Login.html";
    }
    
    /**
     * 弹窗登录
     * @param map
     * @param request
     * @return
     */
    @GetMapping(value = "/login_dialog")
    public String onDialogLoginLoad(ModelMap map, HttpServletRequest request) {
        String path = this.getClass().getResource("/rsakey/publicKey.txt").getFile();
        String publicKey = RSAUtil.readTxt(path);
        map.put("publicKey", publicKey);
        return "DialogLogin.html";
    }
    
    /**
     * 登录成功
     * @param map
     * @param request
     * @return
     */
    @GetMapping(value = "/login_success")
    @ResponseBody
    public InterfaceResult<Map<String, Object>> loginSuccess(ModelMap map, HttpServletRequest request) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        result.setMsg("登录成功！");
        return result;
    }
    
    /**
     * 登录失败
     * @param map
     * @param request
     * @return
     */
    @GetMapping(value = "/login_fail")
    @ResponseBody
    public InterfaceResult<Map<String, Object>> loginFail(ModelMap map, HttpServletRequest request) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        HttpSession session = request.getSession(false);
        result.setError("账号或密码错误！");
        if (session!=null) {
            AuthenticationServiceException obj = (AuthenticationServiceException) session.getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
            result.setError(obj.getMessage());
        }
        return result;
    }
    
    /**
     * @Description: 获取验证码
     * @param request
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/validatecode")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 设置请求头为输出图片类型
        response.setContentType("image/jpeg");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        
        // 算术类型(Java图形验证码，支持gif、中文、算术等类型)
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
        captcha.setLen(2);  // 几位数运算，默认是两位
        captcha.getArithmeticString(); // 获取运算的公式
        captcha.text();  // 获取运算的结果
        request.getSession().setAttribute("validateCode", captcha.text().toLowerCase());
        captcha.out(response.getOutputStream());
    }
}
